package com.handydata.antifraud.ws.dap;

import com.alibaba.fastjson.JSONObject;
import com.handydata.antifraud.annotation.Comment;
import com.handydata.antifraud.common.constant.DataSourceCode;
import com.handydata.antifraud.common.exception.WsInvokeException;
import com.handydata.antifraud.to.dap.HlzBin13ScoreReqTO;
import com.handydata.antifraud.to.dap.HlzBin13ScoreResTO;
import com.handydata.antifraud.util.DateUtil;
import com.handydata.antifraud.util.encrypt.Md5Utils;
import com.handydata.antifraud.ws.WsInvocationService;

import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;

import java.util.Date;

/**
 * 行列秩评分1
 *
 */
@Slf4j
@Service("hlzBin13ScoreWsService")
@Comment("行列秩评分1")
public class HlzBin13ScoreWsServiceImpl extends
		DataPlatformWsService<HlzBin13ScoreReqTO, HlzBin13ScoreResTO> implements
		WsInvocationService<HlzBin13ScoreReqTO, HlzBin13ScoreResTO> {

	@Override
	public HlzBin13ScoreResTO query(HlzBin13ScoreReqTO req) throws WsInvokeException {
		log.info("行列秩评分1查询请求参数 {}", req);
		HlzBin13ScoreResTO resTO = new HlzBin13ScoreResTO();
		if (StringUtils.isEmpty(req.getName())) {
			log.error("行列秩评分1查询参数校验 {}", "name为空");
			resTO.setResCode("1111");
			resTO.setResMsg("行列秩评分1查询接口name参数为空");
			return resTO;
		}
		if (StringUtils.isEmpty(req.getIdCard())) {
			log.error("行列秩评分1查询参数校验 {}", "idCard为空");
			resTO.setResCode("1111");
			resTO.setResMsg("行列秩评分1查询接口idCard参数为空");
			return resTO;
		}
		if (StringUtils.isEmpty(req.getPhoneNum())) {
			log.error("行列秩评分1查询参数校验 {}", "mobile为空");
			resTO.setResCode("1111");
			resTO.setResMsg("行列秩评分1查询接口mobile参数为空");
			return resTO;
		}
		this.dataType = DataSourceCode.HLZ_BIN_13.getCode();
		resTO = executeQuery(req);
		return resTO;
	}

	@Override
	protected String createQueryXml(HlzBin13ScoreReqTO req) {
		StringBuilder xml = new StringBuilder();
		xml.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>");
		xml.append("<subatm>");
		xml.append("<application>GwBiz.Req</application>");
		xml.append("<version>2.0.0</version>");
		xml.append("<sendTime>").append(DateUtil.now14()).append("</sendTime>");
		xml.append("<transCode>100087</transCode>");
		xml.append("<channelId>").append(encryInfo.getChannelId()).append("</channelId>");
		xml.append("<channelOrderId>").append(DateUtil.now17()).append("</channelOrderId>");
		xml.append("<cid>").append(Md5Utils.md5ToHexStr(req.getIdCard())).append("</cid>");
		xml.append("<name>").append(Md5Utils.md5ToHexStr(req.getName())).append("</name>");
		xml.append("<phone>").append(Md5Utils.md5ToHexStr(req.getPhoneNum())).append("</phone>");
//		xml.append("<cid>").append(req.getIdCard()).append("</cid>");
//		xml.append("<name>").append(req.getName()).append("</name>");
//		xml.append("<phone>").append(req.getPhoneNum()).append("</phone>");
		xml.append("<type>SCORE1</type>");
		xml.append("</subatm>");
		log.info("行列秩评分1查询调用翰迪数据平台请求报文{}", xml);
		req.setQueryXml(xml.toString());
		return xml.toString();
	}

	@Override
	protected HlzBin13ScoreResTO parseResultAndSave(String resultXml, String orderId, Date createDate) {
		log.info("调用行列秩评分1查询接口返回报文：{}", resultXml);
		HlzBin13ScoreResTO resTO = new HlzBin13ScoreResTO();
		try {
			// 解析xml，保存本地DB
			JSONObject jasonObject = JSONObject.parseObject(resultXml);
			if (null != jasonObject) {
				if ("0000".equals(jasonObject.get("resCode"))) {
					JSONObject data = JSONObject.parseObject(jasonObject.getString("resData"));
					double hlzBin13Score = Double.parseDouble(data.getString("score"));
					resTO.setHlzBin13Score(hlzBin13Score);
				}
				resTO.setResCode((String) jasonObject.get("resCode"));
				resTO.setResMsg((String) jasonObject.get("resMsg"));
			} else {
				resTO.setResCode("0001");
				resTO.setResMsg("行列秩评分1查询接口返回失败");
			}
		} catch (Exception e) {
			log.info("调用行列秩评分1查询接口返回报文解析异常：{}", e);
			resTO.setResCode("1111");
			resTO.setResMsg("调用行列秩评分1查询接口异常");
		}
		log.info("调用行列秩评分1查询接口返回结果：{}", JSONObject.toJSONString(resTO));
		return resTO;
	}

	@SuppressWarnings("unchecked")
	@PostConstruct
	@Override
	public void register() {
		registerToWsServiceFactory(this, HlzBin13ScoreReqTO.class);
	}

}
